package top.yxqz.sprinmvc.restful.dao;

import org.springframework.stereotype.Repository;
import top.yxqz.sprinmvc.restful.pojo.Employee;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * 以前的思路：
 * Controller——————自动装配Service（实际上自动装配的是Service的实现类）——————Service的实现类——————自动装配Dao接口（实际上自动装配的是Dao接口的实现类）————数据层操作数据库（执行SQL）
 * Controller—————自动装配Dao接口（执行SQL）
 */
@Repository
public class EmployeeDao {
    private static Map<Integer, Employee> employees = null;

    static {
        employees = new HashMap<Integer, Employee>();
        employees.put(1001, new Employee(1001, "YZQZ-001", "123456@qq.com", 1));
        employees.put(1002, new Employee(1002, "YZQZ-002", "153456@qq.com", 1));
        employees.put(1003, new Employee(1003, "YZQZ-003", "133456@qq.com", 0));
        employees.put(1004, new Employee(1004, "YZQZ-004", "163456@qq.com", 0));
        employees.put(1005, new Employee(1005, "YZQZ-004", "18456@qq.com", 1));
    }

    private static Integer initId = 1006;

    /**
     * 实现添加|修改
     */
    public void save(Employee employee) {
        if (employee.getId() == null) {
            employee.setId(initId++);
        }
        employees.put(employee.getId(), employee);
    }


    /**
     * 得到所有员工对象
     *
     * @return
     */
    public Collection<Employee> getAll() {
        return employees.values();
    }

    /**
     * 通过id得到员工对象
     *
     * @param id
     * @return
     */
    public Employee get(Integer id) {
        return employees.get(id);
    }

    /**
     * 通过id删除员工对象
     *
     * @param id
     */
    public void delete(Integer id) {
        employees.remove(id);
    }
}